package com.seed.service.sysmanager;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.seed.commons.dbquery.PageModelForSQL;
import com.seed.commons.dbutil.DB;
import com.seed.commons.dbutil.DBBean;
import com.seed.commons.util.RandomUtil;
import com.seed.web.form.sysmanager.MenuItemForm;

@Service
public class MenuItemService implements IMenuItem{

	private Log log = LogFactory.getLog(getClass()) ;
 	
	@Override
	public boolean saveMenuItem(MenuItemForm mi) {
		String sql = "INSERT INTO SEED_MENU_ITEM(MLId,MLName,MLIsRoot,MLUrl,MLDesc,MLIcon,MLFunbar,SEED_MENU_MODEL_MMId_FK,SEED_MENU_ITEM_MLId_FK) VALUES(?,?,?,?,?,?,?,?)" ; 
		Object[] val = new Object[]{RandomUtil.getRandomNum(5),mi.getMlName(),mi.getMlUrl(),mi.getMlDesc(),mi.getMlIcon(),mi.getMlFunbar(),mi.getMenuModelFK(),mi.getMenuItemFk()};
		
		try {
			DB.insert(sql, val) ;
			return true ;
		} catch (Exception e) {
			log.info("数据插入出错：" + e.getMessage()) ;
			log.info("执行SQL：" + sql) ;
		}
		return false ;
		
	}

	@Override
	public void deleteMenuItem(String id) {
		
		
	}

	@Override
	public void updateMenuItem(MenuItemForm mi) {
		
		
	}

	@Override
	public DBBean queryMenuItemById(String id) {
		
		
		return null;
	}

	@Override
	public PageModelForSQL getScrollData(int currentPage, int lineSize) {
		
		return null;
	}

	@Override
	public List<DBBean> queryMenuItem(Map<String,Object[]> mapKey) {
		
		String sql = "SELECT MLId,MLName,MLIsRoot,MLUrl,MLDesc,MLIcon,MLFunbar,SEED_MENU_MODEL_MMId_FK,SEED_MENU_ITEM_MLId_FK,ADDDATE FROM SEED_MENU_ITEM" ;
		
		Object[] queryParam = null ;
		
		if(null != mapKey && !mapKey.isEmpty()) {
			for(String mkey : mapKey.keySet()) {
				if("BYMenuItemRoot".equals(mkey))
					sql = "SELECT MLId,MLName,MLIsRoot,MLUrl,MLDesc,MLIcon,MLFunbar,SEED_MENU_MODEL_MMId_FK,SEED_MENU_ITEM_MLId_FK,TO_CHAR(ADDDATE,'yyyy-MM-DD') ADDDATE FROM SEED_MENU_ITEM WHERE SEED_MENU_MODEL_MMId_FK=? AND MLIsRoot=?" ;
				queryParam = mapKey.get(mkey) ;
				if("BYMenuItemFK".equals(mkey))
					sql = "SELECT MLId,MLName,MLIsRoot,MLUrl,MLDesc,MLIcon,MLFunbar,SEED_MENU_MODEL_MMId_FK,SEED_MENU_ITEM_MLId_FK,TO_CHAR(ADDDATE,'yyyy-MM-DD') ADDDATE FROM SEED_MENU_ITEM WHERE SEED_MENU_ITEM_MLId_FK=?" ;
					queryParam = mapKey.get(mkey) ;
				if("showMenuList".equals(mkey))
					sql = "" +
							"SELECT S2.*,("+
							"	SELECT SS2.MLNAME "+ 
							"	FROM SEED_MENU_ITEM SS2 "+ 
							"	WHERE SS2.MLID=S2.SEED_MENU_ITEM_MLID_FK) AS PARENT_NAME "+ 
							"FROM SEED_MENU_ITEM S2 "+ 
							"WHERE S2.SEED_MENU_ITEM_MLID_FK IN("+ 
							"	SELECT S1.MLID "+ 
							"	FROM SEED_MENU_ITEM S1 "+ 
							"	WHERE S1.MLISROOT = 'Y' AND S1.SEED_MENU_MODEL_MMID_FK=?"+ 
							")";
					queryParam = mapKey.get(mkey) ;
			}
		}
		
		try {
			return DB.query(sql, queryParam) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
